package org.rsbot.script.randoms;

import java.util.logging.Logger;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSObject;

@ScriptManifest(authors={"Qauters", "Drizzt1112", "TwistedMind"}, name="SandwichLady", version=2.3D)
public class SandwhichLady extends Random
{
  static final int ID_InterfaceSandwhichWindow = 297;
  static final int ID_InterfaceSandwhichWindowText = 48;
  static final int ID_InterfaceTalk = 243;
  static final int ID_InterfaceTalkText = 7;
  static final int[] ID_Items = { 10728, 10732, 10727, 10730, 10726, 45666, 10731 };
  static final int ID_SandwhichLady = 8630;
  static final String[] Name_Items = { "chocolate", "triangle", "roll", "pie", "baguette", "doughnut", "square" };
  final boolean DEBUG = false;

  public boolean activateCondition()
  {
    RSNPC localRSNPC = this.npcs.getNearest(new int[] { 8630 });
    return localRSNPC != null;
  }

  public int loop()
  {
    RSInterface localRSInterface = this.interfaces.get(243);
    if (localRSInterface.isValid()) {
      this.interfaces.getComponent(243, 7).doClick();
      return random(900, 1200);
    }
    if (!activateCondition()) {
      return -1;
    }

    int[] arrayOfInt = { 12731, 11373 };
    if (this.interfaces.get(242).getComponent(4).containsText("The exit portal's")) {
      localObject = this.objects.getNearest(arrayOfInt);
      if (localObject != null) {
        if (!this.calc.tileOnScreen(((RSObject)localObject).getLocation())) {
          this.walking.walkTileOnScreen(((RSObject)localObject).getLocation());
        } else {
          ((RSObject)localObject).doAction("Enter");
          return random(1000, 1500);
        }
      }
    }

    if (this.interfaces.get(243).isValid()) {
      this.interfaces.getComponent(243, 7).doClick();
      return random(900, 1200);
    }

    if (this.interfaces.get(297).isValid()) {
      localObject = this.interfaces.get(297);
      int i = -1;
      String str = ((RSInterface)localObject).getComponent(48).getText();
      for (int j = 0; j < Name_Items.length; j++) {
        if (str.contains(Name_Items[j])) {
          i = j;
        }

      }

      for (j = 7; j < 48; j++) {
        RSComponent localRSComponent = ((RSInterface)localObject).getComponent(j);

        if (localRSComponent.getModelID() == ID_Items[i]) {
          localRSComponent.doClick();
          sleep(random(900, 1200));
          if (!this.interfaces.get(297).isValid()) {
            this.log.info("Solved the Sandwich Lady, by eating a " + Name_Items[i]);
            sleep(6000);
            return random(900, 1500);
          }
        }
      }

      return random(900, 1200);
    }
    Object localObject = this.interfaces.get(242);
    if (((RSInterface)localObject).isValid()) {
      this.interfaces.getComponent(242, 6).doClick();
      return random(900, 1200);
    }

    RSNPC localRSNPC = this.npcs.getNearest(new int[] { 8630 });
    if ((localRSNPC != null) && (localRSNPC.getAnimation() == -1)) {
      if (!this.calc.tileOnScreen(localRSNPC.getLocation())) {
        this.walking.walkTileOnScreen(localRSNPC.getLocation());
      } else {
        localRSNPC.doAction("talk");
        return random(1000, 1500);
      }
    }
    return random(900, 1200);
  }
}